package com.sisy.service.impl;

import com.sisy.common.RefundOrder;
import com.sisy.common.RefundOrderService;
import com.sisy.common.RefundOrderTrade;
import com.sisy.mapper.RefundOrderTradeMapper;
import com.sisy.service.RefundService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalLock;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author: xx
 * @date: 2021-02-04 11:27
 */
@Service
public class RefundServiceImpl implements RefundService {
    @Autowired
    private RefundOrderTradeMapper refundOrderTradeMapper;
    @Autowired
    private RefundOrderService refundOrderService;

    @Override
    @GlobalTransactional
    public void refund(int id, BigDecimal refundAmount) {
        System.out.println(" -=-=-=-=- 全局事务id ：" + RootContext.getXID());
        RefundOrderTrade refundOrderTrade = new RefundOrderTrade();
        refundOrderTrade.setRefundOrderId(id);
        refundOrderTrade.setTradeAmount(refundAmount);
        refundOrderTradeMapper.insertRefundTrade(refundOrderTrade);
        System.out.println("come in -------");
        refundOrderService.updateRefundOrder(id, refundAmount);
        try {
            Thread.sleep(10*1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = 1/0;
    }

    @Override
    @GlobalTransactional
    public RefundOrder getRefundOrderById(int id) {
        RefundOrder refundOrderById = refundOrderService.getRefundOrderById(id);
        return refundOrderById;
    }
}
